﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCS.Core.Models;
using SCS.Core.ViewModels;
using SCS.Core.Models.ModelsFontend;
using SCSService;

namespace SCS.Core.Controllers
{
    public class ShoppingCartController : BaseFontendController
    {
        //
        // GET: /ShoppingCart/

        public ActionResult Cart()
        {
            IntPage();
            return View();
        }

        [ChildActionOnly]
        public ActionResult Carts()
        {
            int? languageId = StockModel.GetLanguageIDByCountryCode(DefaultLanguageGetter());
            var cart = ShoppingCart.GetCart(this.HttpContext);

            // Set up our ViewModel
            var viewModel = new ShoppingCartViewModels
            {
                CartItems = cart.GetListCartItemByCartID(),
                CartTotal = cart.GetTotalValueCartItem(languageId)
            };

            return PartialView("../BaseFontend/" + GetSiteGuid() + "/Carts", viewModel);
        }


        public ActionResult AddToCart(int id)
        {

            // get Product
            var product = ProductModel.GetProductByID(id);

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(product);

            // Go back to the main store page for more shopping
            return RedirectToAction("Cart");
        }

        public ActionResult AddMultiToCart(string id)
        {
            var arr = id.Split(',');
            // get Product
            var product = ProductModel.GetProductByID(int.Parse(arr[0]));

            if (arr[1] != null && arr[1] != string.Empty)
            {
                int no = int.Parse(arr[1]);
                if (no > 0)
                {
                    for (int i = 0; i < no; i++)
                    {
                        // Add it to the shopping cart
                        var cart = ShoppingCart.GetCart(this.HttpContext);

                        cart.AddToCart(product);
                    }
                    
                }
            }
            

            // Go back to the main store page for more shopping
            return Json("1");
        }

        [HttpPost]
        public ActionResult UpdateToCart(string id)
        {
            var arr = id.Split(',');
            // get Product
            var product = ProductModel.GetProductByID(int.Parse(arr[0]));

            // Add it to the shopping cart
            var cart = ShoppingCart.GetCart(this.HttpContext);


            var cartItem = ShoppingCart.GetCartbyRecordID(int.Parse(arr[2]));
            // Get product
            if (cartItem != null)
            {
                //var productDelete = ProductModel.GetProductByID((int)cartItem.ProductID);

                // Remove from cart. Note that for simplicity, we're 
                // removing all rather than decrementing the count.
                for (int i = 0; i < cartItem.Count; i++)
                {
                    cart.RemoveFromCart(int.Parse(arr[2]));
                }

            }


            if (int.Parse(arr[1]) > 1)
            {
                for (int i = 0; i < int.Parse(arr[1]); i++)
                {
                    cart.AddToCart(product);
                }
            }
            else
                cart.AddToCart(product);


            // Go back to the main store page for more shopping
            string result = "" + int.Parse(arr[1]);
            return Json(result);
        }


       


        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            int? languageId = null;
            // Remove the item from the cart
            var cart = ShoppingCart.GetCart(this.HttpContext);

            var cartItem = ShoppingCart.GetCartbyRecordID(id);
            // Get product
            var product = ProductModel.GetProductByID((int)cartItem.ProductID);

            // Remove from cart. Note that for simplicity, we're 
            // removing all rather than decrementing the count.
            //cart.RemoveFromCart(id);
            for (int i = 0; i < cartItem.Count; i++)
            {
                cart.RemoveFromCart(id);
            }

            // Display the confirmation message
            var results = new ShoppingCartRemoveViewModels
            {
                Message = Server.HtmlEncode(product.ProductName) +
                    " đã được xóa khỏi giỏ hàng của bạn.",
                CartTotal = cart.GetTotalValueCartItem(languageId),
                CartCount = cart.GetCountCartItem(),
                DeleteId = id
            };

            return Json(results);
        }
    }
}
